package com.example.tests;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

import org.testng.annotations.Test;

import com.example.fw.HelperBase;
import com.example.utils.SortedListOf;

public class GroupModificationTests extends TestBase {

	@Test(dataProvider = "randomValidGroupGenerator")
	public void modifySomeGroup(GroupData group) {

		//save old state
//		SortedListOf<GroupData> oldList = new SortedListOf<GroupData>(app.getHibernateHelper().listGroups());
		SortedListOf<GroupData> oldList = app.getModelGroups().getGroups();

    int index = app.getGroupHelper().getRandomGroupIndex(oldList);    	
    if (index >= 0) {
	 		if (index % 2 == 0) {
	 			// empty fields of group do not overwrite old values 
		    app.getGroupHelper().modifyGroup(index, group, HelperBase.KEEP);

		    // save new state
//		    SortedListOf<GroupData> newList = app.getGroupHelper().getUiGroups();
		    SortedListOf<GroupData> newList = app.getModelGroups().getGroups();
		    
		    // compare state states before and after
		    assertThat(newList, equalTo(oldList));
	 		} else {
	 			// empty fields of group overwrite old values 
		    app.getGroupHelper().modifyGroup(index, group, HelperBase.OVERWRITE);

		    // save new state
//		    SortedListOf<GroupData> newList = app.getGroupHelper().getUiGroups();
		    SortedListOf<GroupData> newList = app.getModelGroups().getGroups();
		    
		    // compare state states before and after
		    assertThat(newList, equalTo(oldList));
	 		}
	 		
	    // compare model to implementation
	    if (wantToCheck()) {
	      if ("yes".equals(app.getProperty("check.db"))) {
	        assertThat(app.getModelGroups().getGroups(), equalTo(app.getHibernateHelper().listGroups()));
	        System.out.println("DB check done. On group: " + group.getGrName());
	      }
	      
	      if ("yes".equals(app.getProperty("check.ui"))) {
	        assertThat(app.getModelGroups().getGroups(), equalTo(app.getGroupHelper().getUiGroups()));
	        System.out.println("UI check done. On group: " + group.getGrName());
	      }
	    }
	 		
		}
	}

}
